package com.ming.service.impl;

import com.ming.dao.CartDao;
import com.ming.po.Cart;
import com.ming.po.ResponseBean;
import com.ming.service.CartService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	private CartDao cd;

	@Override
	@Transactional
	public ResponseBean<Integer> saveCart(Cart cart) {
		int result = 0;
		ResponseBean<Integer> rb;
		result = cd.saveCart(cart);

		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"保存失败");
		}
		return rb;
	}
	
	@Override
	@Transactional
	public ResponseBean<Integer> updateCart(Cart cart) {
		int result = 0;
		ResponseBean<Integer> rb;
		result = cd.updateCart(cart);

		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"保存失败");
		}
		return rb;
	}
	
	@Override
	@Transactional
	public ResponseBean<Integer> removeCart(Cart cart) {
		int result = 0;
		ResponseBean<Integer> rb;
		result = cd.removeCart(cart);

		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"保存失败");
		}

		return rb;
	}
	
	@Override
	public ResponseBean<List<Cart>> listCart(Cart cart){
		return new ResponseBean<>(cd.listCart(cart));
	}
}
